<?php
Class Brand_model extends CI_Model
{

    function get_brands($parent = false)
    {
        if ($parent !== false)
        {
            $this->db->where('parent_id', $parent);
        }
        $this->db->select('id');
        $this->db->order_by('brands.sequence', 'ASC');
        
        //this will alphabetize them if there is no sequence
        $this->db->order_by('name', 'ASC');
        $result = $this->db->get('brands');
        
        $brands = array();
        foreach($result->result() as $cat)
        {
            $brands[]   = $this->get_brand($cat->id);
        }
        
        return $brands;
    }
    
    function get_brands_tiered($admin = false)
    {
        if(!$admin) $this->db->where('enabled', 1);
        
        $this->db->order_by('sequence');
        $this->db->order_by('id', 'ASC');
        $brands = $this->db->get('brands')->result();
        
        $results    = array();
        foreach($brands as $brand) {

            // Set a class to active, so we can highlight our current brand
            if($this->uri->segment(1) == $brand->slug) {
                $brand->active = true;
            } else {
                $brand->active = false;
            }

            $results[0][$brand->id] = $brand;
        }
        
        return $results;
    }
    
    function get_brand($id)
    {
        return $this->db->get_where('brands', array('id'=>$id))->row();
    }
	
	function get_brand_from_slug($slug)
    {
        return $this->db->get_where('brands', array('slug'=>$slug))->row();
    }
    
    function get_brand_products_admin($id)
    {
        $this->db->order_by('sequence', 'ASC');
        $result = $this->db->get_where('brand_products', array('brand_id'=>$id));
        $result = $result->result();
        
        $contents   = array();
        foreach ($result as $product)
        {
            $result2    = $this->db->get_where('products', array('id'=>$product->product_id));
            $result2    = $result2->row();
            
            $contents[] = $result2; 
        }
        
        return $contents;
    }
    
    function get_brand_products($id, $limit, $offset)
    {
        $this->db->order_by('sequence', 'ASC');
        $result = $this->db->get_where('brand_products', array('brand_id'=>$id), $limit, $offset);
        $result = $result->result();
        
        $contents   = array();
        $count      = 1;
        foreach ($result as $product)
        {
            $result2    = $this->db->get_where('products', array('id'=>$product->product_id));
            $result2    = $result2->row();
            
            $contents[$count]   = $result2;
            $count++;
        }
        
        return $contents;
    }
    
    function organize_contents($id, $products)
    {
        //first clear out the contents of the brand
        $this->db->where('brand_id', $id);
        $this->db->delete('brand_products');
        
        //now loop through the products we have and add them in
        $sequence = 0;
        foreach ($products as $product)
        {
            $this->db->insert('brand_products', array('brand_id'=>$id, 'product_id'=>$product, 'sequence'=>$sequence));
            $sequence++;
        }
    }
    
    function save($brand)
    {
        if ($brand['id'])
        {
            $this->db->where('id', $brand['id']);
            $this->db->update('brands', $brand);
            
            return $brand['id'];
        }
        else
        {
            $this->db->insert('brands', $brand);
            return $this->db->insert_id();
        }
    }
    
    function delete($id)
    {
        $this->db->where('id', $id);
        $this->db->delete('brands');
        
        //delete references to this brand in the product to brand table
        $this->db->where('brand_id', $id);
        $this->db->delete('brand_products');
    }
}